import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_taotaoshipin/common/theme/app_colors.dart';
import 'package:flutter_taotaoshipin/common/theme/dimens.dart';
import 'package:flutter_taotaoshipin/util/theme_utils.dart';
import 'package:flutter_taotaoshipin/widget/gaps.dart';
import 'package:flutter_taotaoshipin/widget/load_image.dart';
import 'package:flutter_taotaoshipin/widget/my_button.dart';
import 'package:get/get.dart';

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const MyAppBar({
    super.key,
    this.backgroundColor,
    this.title = '',
    this.centerTitle = '',
    this.backImg = 'nav_back',
    this.backImgColor,
    this.actionImage,
    this.actionName = '',
    this.onPressed,
    this.isBack = true,
    this.hideLine = false,
  });

  final Color? backgroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final Color? backImgColor;
  final String actionName;
  final String? actionImage;
  final VoidCallback? onPressed;
  final bool isBack;
  final bool hideLine;

  @override
  Widget build(BuildContext context) {
    // 背景颜色
    final Color bgColor = backgroundColor ?? context.appbarBackgroundColor;
    // 状态栏颜色
    final SystemUiOverlayStyle systemUiOverlayStyle =
        ThemeData.estimateBrightnessForColor(bgColor) == Brightness.dark
            ? SystemUiOverlayStyle.light
            : SystemUiOverlayStyle.dark;
    // 右边按钮
    final Widget action = actionImage != null
        ? Positioned(
            right: 0,
            child: InkWell(
              onTap: onPressed,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 16.0,vertical: 10),
                child: LoadAssetImage(
                  actionImage!,
                  width: 24,
                ),
              ),
            ),
          )
        : actionName.isNotEmpty
            ? Positioned(
                right: 0,
                child: Theme(
                  data: Theme.of(context).copyWith(
                    buttonTheme: const ButtonThemeData(
                      padding: EdgeInsets.symmetric(horizontal: 16.0),
                      minWidth: 60.0,
                    ),
                  ),
                  child: MyButton(
                    key: const Key('actionName'),
                    text: actionName,
                    textColor: Colors.black87,
                    fontSize: Dimens.font_sp14,
                    backgroundColor: Colors.transparent,
                    onPressed: onPressed,
                    minWidth: null,
                  ),
                ),
              )
            : Gaps.empty;
    // 左边返回按钮
    final Widget back = isBack
        ? IconButton(
            padding: const EdgeInsets.all(12.0),
            tooltip: 'Back',
            onPressed: () {
              // 失去焦点
              FocusManager.instance.primaryFocus?.unfocus();
              Get.back();
            },
            icon: LoadAssetImage(
              backImg,
              color: backImgColor,
              width: 16,
            ),
          )
        : Gaps.empty;
    // 标题
    final titleWidget = Semantics(
      namesRoute: true,
      header: true,
      child: Container(
        alignment:
            centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
        width: double.infinity,
        margin: const EdgeInsets.symmetric(horizontal: 48.0),
        child: Text(
          title.isEmpty ? centerTitle : title,
          style: const TextStyle(
            fontSize: 18,
            color: Color(0xFF333333),
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
    );
    return AnnotatedRegion(
      value: systemUiOverlayStyle,
      child: Material(
        color: bgColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: [
              titleWidget,
              back,
              action,
              if (!hideLine)
                Positioned(
                  left: 0,
                  bottom: 0,
                  right: 0,
                  child: Gaps.line,
                ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(48.0);
}
